package NewCode;


/**
 * @author wangchenghao
 * @date 2021/7/23
 **/
public class NC66 {
    public class ListNode {
        ListNode next = null;
        int data;

        public ListNode(int data) {
            this.data = data;
        }

        public ListNode() {

        }
    }

    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode l1 = pHead1;
        ListNode l2 = pHead2;
        int length1 = 0, length2 = 0;
        while (l1 != null || l2 != null) {
            if (l1 != null) {
                l1 = l1.next;
                length1++;
            } else {
                length2++;
            }
            if (l2 != null) {
                l2 = l2.next;
                length2++;
            } else {
                length1++;
            }
        }
        l1 = pHead1;
        l2 = pHead2;
        while (length1 != 0) {
            l1 = l1.next;
            length1--;
        }
        while (length2 != 0) {
            l2 = l2.next;
            length2--;
        }

        while (l1 != l2) {
            l1 = l1.next;
            l2 = l2.next;
        }
        return l1;
    }
}
